import Toast from '../../component/vant/toast/toast';

const app = getApp();
const api = require('../../api.js');
const util = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    domain: api.xh.downloadDomain,
    courseSelect: '', //
    subjectSelect: '',
    selectGrade: '',
    selectSubject: '',
    show: false,
    selected: 0,
    hot_courses: [],
    last_id: '',
    teacherTagStyle: util.teacherTagStyles,
    f_type: 0,
    show_sm_filter: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let grade_id = options.grade_id ? options.grade_id : '';
    let course_id = options.course_id ? options.course_id : '';
    var page = this;

    this.changeParentData();
    app.sendRequest({
      url: api.public.grade_course_student,
      success: function(res) {
        if (res.data != '') {
          let grade_courses = util.getCourses(res.data.list);
          // 设置数据
          grade_courses.map(g_course => {
            if (g_course.grade_id == grade_id) {
              page.setData({
                course: grade_courses,
                subject: g_course.course_list
              });
            }
          });
        }
      }
    });

    page.setData({
      courseSelect: grade_id,
      subjectSelect: course_id
    });
    app.setPageTitle(wx.getStorageSync('gradeNameSelect'));
    //  课程列表
    this.getCourseList();
  },
  toggleFilter() {
    let page = this;
    page.setData({
      show_sm_filter: !page.data.show_sm_filter
    });
  },
  frmClick(e) {
    app.saveFormId(e.detail.formId);
  },
  changeParentData: function() {
    var pages = getCurrentPages(); //当前页面栈
    if (pages.length > 2) {
      var beforePage = pages[pages.length - 2]; //获取上一个页面实例对象
      if (beforePage.route.indexOf('filter') != -1) {
        beforePage.changeData(); //触发父页面中的方法
      }
    }
  },
  swichNav(e) {
    let page = this,
      course_id = e.currentTarget.dataset.id;
    page.setData({
      hot_courses: [],
      last_id: '',
      subjectSelect: course_id
    });
    page.getCourseList();
  },
  getFiltedData(e) {
    let page = this,
      type = e.currentTarget.dataset.type;
    page.setData({
      hot_courses: [],
      last_id: '',
      f_type: type
    });
    page.getCourseList();
  },
  //  课程列表
  getCourseList: function() {
    var page = this;

    let param = {
      grade_id: page.data.courseSelect == '' ? 0 : page.data.courseSelect,
      course_id: page.data.subjectSelect == '' ? 0 : page.data.subjectSelect,
      order: page.data.f_type == '' ? 0 : page.data.f_type
    };

    //已登录情况下的课程已学未学筛选
    if (page.data.is_login) {
      param.if_study = page.data.checked ? 1 : 0;
    }

    if (page.data.last_id > -1 && page.data.last_id != '') {
      param.last_id = page.data.last_id;
    } else {
      if (page.data.last_id == -1) {
        page.data.hot_courses.length > 0 && Toast('没有更多了');
        return false;
      }
    }

    // 判断是否登陆过调用不同接口
    let url = page.data.is_login
      ? api.course.student_curriculum_list_login
      : api.course.student_curriculum_list;

    app.sendRequest({
      url: url,
      data: param,
      success: function(res) {
        var course_list = page.data.hot_courses;
        var course_list_load = res.data.list;

        course_list_load == undefined ? [] : course_list_load;
        var last_id = 0;

        if (course_list_load != undefined && course_list_load.length > 0) {
          last_id = res.data.last_id;
          course_list = course_list.concat(course_list_load);
          page.setData({
            hot_courses: course_list,
            last_id: last_id
          });
        }
      }
    });
  },

  toggleCourse: function() {
    var page = this;
    page.setData({
      show: !page.data.show
    });
  },

  sureChoose: function() {
    var page = this;
    page.setData({
      hot_courses: [],
      last_id: '',
      show: false
    });
    //  课程列表
    this.getCourseList();
  },

  chooseGrade: function(e) {
    var page = this;
    if (page.data.selected == 0) {
      page.setData({
        selected: 1
      });
    }

    page.setData({
      selectGrade:
        e.currentTarget.dataset.select == ''
          ? '全部'
          : e.currentTarget.dataset.name,
      courseSelect: e.currentTarget.dataset.select
    });
  },

  chooseSubject: function(e) {
    var page = this;
    if (page.data.selected == 0) {
      page.setData({
        selected: 1
      });
    }
    page.setData({
      selectSubject:
        e.currentTarget.dataset.select == ''
          ? ''
          : e.currentTarget.dataset.name,
      subjectSelect: e.currentTarget.dataset.select
    });
  },

  toCourse: function(e) {
    let page = this;
    var cid = e.currentTarget.dataset.id;
    var curriculum_name = e.currentTarget.dataset.curriculum_name;
    app.checkSession();
    if (!wx.getStorageSync('_logining')) {
      wx.setStorageSync(
        'auth_url',
        '/' +
          this.route +
          '?grade_id=' +
          page.data.courseSelect +
          '&course_id=' +
          page.data.subjectSelect
      );
      app.toAuth('/' + this.route, 1);
    } else {
      wx.navigateTo({
        url:
          '/pages/course_detail/course_detail?curriculum_id=' +
          cid +
          '&curriculum_name=' +
          curriculum_name
      });
    }
  },
  onChange(e) {
    let page = this;
    page.setData({
      checked: e.detail,
      hot_courses: [],
      last_id: ''
    });

    page.getCourseList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let page = this,
      is_login = wx.getStorageSync('_logining');
    page.setData({
      is_login: is_login
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.getCourseList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
